package com.sxt;



import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;


public class GameWin extends JFrame {
    //状态 0未开始 1运行中 2商店 3失败 4胜利
    static int state;
    Image offScreenImage;
    //存储金块,石块
    List<Object> objectList = new ArrayList<Object>();
    Bj bj = new Bj();

    Line line = new Line(this);

    {
        //是否可以放置
        boolean isPlace = true;
        for (int i = 0; i < 11; i++) {
            double random = Math.random();
            Gold gold ;//存放当前生成的金块
            if (random < 0.3){
                gold = new GoldMini();
            }else if (random < 0.7){
                gold = new Gold();
            }else {
                gold = new GoldPlus();
            }
            for (Object object : objectList) {
                //用来判断物体是否堆积在一起
                if (gold.getRec().intersects(object.getRec())){
                    //不可放置 重新生成
                    isPlace = false;
                }
            }
            if (isPlace){
                objectList.add(gold);
            }else {
                isPlace = true;
                i--;
            }
        }
        for (int i = 0; i < 3; i++) {

            objectList.add(new Rock());

        }
    }


    void launch(){
        this.setVisible(true);
        this.setSize(768,1000);
        this.setLocationRelativeTo(null);
        this.setTitle("黄金矿工");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                switch (state){
                    case 0:
                        if (e.getButton()==1 ||e.getButton()==3){
                            state = 1;
                            bj.startTime = System.currentTimeMillis();
                        }
                        break;
                    case 1:
                        //左右摇摆 左键
                        if (e.getButton() == 1 && line.state == 0){//左键是1 右键是3 滚轮是2
                            line.state = 1;
                        }
                        //抓取返回 右键
                        if (e.getButton() == 3 && line.state == 3 && bj.waterNum>0){
                            bj.waterFlag = true;
                            bj.waterNum--;
                        }
                        break;
                    case 2:
                        if (e.getButton() == 1){
                            bj.shop = true;
                        }
                        if (e.getButton() == 3){
                            state = 1;
                            bj.startTime = System.currentTimeMillis();
                        }
                        break;
                    case 3:

                    case 4:
                        if (e.getButton() == 1){
                            state = 0;
                            bj.reGame();
                            line.reGame();
                        }
                        break;
                    default:
                }
            }
        });

        while (true){
            repaint();
            nextLevel();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public void nextLevel(){
        if (bj.gameTime() && state==1){
            if (bj.count >= bj.goal){
                if (bj.level == 5){
                    state = 4;
                }else {
                    state = 2;
                    bj.level++;
                }
            }else {
                state = 3;
            }
            dispose();
            GameWin gameWin = new GameWin();
            gameWin.launch();
        }
    }
    @Override
    public void paint(Graphics g) {
        offScreenImage = this.createImage(768,1000);
        Graphics gImage = offScreenImage.getGraphics();

        bj.paintSelf(gImage);
        if (state == 1){
            //绘制物体
            for (Object object : objectList) {
                object.paintSelf(gImage);
            }
            line.paintSelf(gImage);
        }

        g.drawImage(offScreenImage,0,0,null);
    }

    public static void main(String[] args) {
        GameWin gameWin = new GameWin();
        gameWin.launch();
    }
}
